package fina2.dcs.i18n;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.international.LocaleSelector;

@Stateless
@Scope(ScopeType.SESSION)
@LocalBinding(jndiBinding = "LanguageSession/local")
@SuppressWarnings("serial")
@Name("langSession")
@TransactionManagement(TransactionManagementType.BEAN)
public class LanguageSession implements LanguageLocal, java.io.Serializable {

	@PersistenceContext
	private EntityManager em;

	private List<SelectItem> langs = new ArrayList<SelectItem>();

	public static final BigDecimal langId = new BigDecimal("1");
	private static String langCode = "en_US";

	private Logger log = Logger.getLogger(getClass());

	@Resource
	private UserTransaction ut;

	@Remove
	public void remove() {

	}

	@SuppressWarnings({ "unchecked" })
	public List<SelectItem> getLangs() {
		try {
			if (langs.size() == 0) {
				log.info("Loading languages");
				langs.clear();
				ut.begin();
				List<Language> langsList = em.createQuery("FROM " + Language.class.getName() + " l ORDER BY l.code").getResultList();
				ut.commit();

				for (int i = 0; i < langsList.size(); i++) {
					if (langsList.get(i).getCode().toLowerCase().trim().equals("en_us")) {
						langs.add(new SelectItem(langsList.get(i).getCode(), langsList.get(i).getName()));
						break;
					}
				}
				for (int i = 0; i < langsList.size(); i++) {
					if (langsList.get(i).getCode().toLowerCase().trim().equals("en_us")) {
						continue;
					}
					langs.add(new SelectItem(langsList.get(i).getCode(), langsList.get(i).getName()));
				}

				LocaleSelector.instance().setLocale(new Locale(langCode));
				LocaleSelector.instance().select();
				// messages = LanguageUtil.loadMessages();
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return langs;
	}

	public void setLangs(List<SelectItem> langs) {
		this.langs = langs;
	}

	public String getLangCode() {
		return langCode;
	}

	public void setLangCode(String lngCode) {
		langCode = lngCode;
	}

	public void setCurrentLocation() {
		if (langCode != null) {
			if (!langCode.toLowerCase().equals("en_us")) {
				LocaleSelector.instance().setLocale(new Locale(langCode.substring(0, 2).toLowerCase()));
			} else {
				LocaleSelector.instance().setLocale(new Locale(langCode));
			}
			LocaleSelector.instance().select();
		}
	}

	public Language findByCode(String code) {
		Language lang = null;
		Query q = em.createQuery("FROM " + Language.class.getName() + " l WHERE RTRIM(l.code)=:code");
		q.setParameter("code", code.trim());
		lang = (Language) q.getSingleResult();
		return lang;
	}

	public Language findById(BigDecimal id) {
		Language lang = null;
		try {
			ut.begin();
			lang = em.find(Language.class, id);
			ut.commit();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return lang;
	}

}
